<?php
/*
    Create Date : 18/10/2010
    Author    : Nguyen Thanh Trung
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    =====================================================
    Update History:
    Ver.    TRB#        Date          Author       Note
    // Báo cáo theo phòng theo thời gian
*/   
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
    //******************** Start xuat Excel ********************//
    //$sSubmitType = $_GET['submittype'];
    $sSubmitType = isset($_GET['submittype'])?$_GET['submittype']:"";
    global  $arrConfig,$NUMBERROW,$sUser,$tplUrlReLoad;
    $sUser = $_SESSION['username'];
    $tblDisplay = 'none';
    $tblExcel = 'none';
    $NUMBERROW = $arrConfig['itemsperpage']; 
    $objXajax->registerFunction('LoadRoom'); 
    $objXajax->registerFunction('ajx_viewchart');  
    if ($sSubmitType == "ToExcel")
    {
        ExportToExcel();
        return;
    } 
    //******************** End xuat Excel ********************//
 
  initPage();
/**
* ham khoi tao cac gia tri cho cac control tren luoi
*
* @return bien du lieu 
*/
 function initPage(){ 
    global $objDbSelect,$objTemplate,$tplFrom,$tplTo;  
    $tplFrom       = SYSTEM_DATE;    
    $tplTo         = SYSTEM_DATE;
    $arrRoomType   =  GetRoomTypeByHotel();
    $objTemplate->MergeBlock('blk_list',$arrRoomType);         
 } 

 
 function GetRoomByHotel($room_type='',$hotel_id=''){
     global $objDbSelect;     
     $hotel_id =  ($hotel_id=='')? $_SESSION['hotel_id']:$hotel_id;
     $condition ='';      
     if($room_type!=''){
       $condition .= " and  room_type = $room_type";  
     }
     $sql =  "SELECT id,room_no as name from ks_room where hotel_id =$hotel_id  $condition "  ;
     $arr = $objDbSelect->GetArray($sql);
     return   $arr;
 }    
 
  function GetRoomTypeByHotel($hotel_id=''){
     global $objDbSelect;
     $_SESSION['hotel_id'] =1;
     $hotel_id =  ($hotel_id=='')? $_SESSION['hotel_id']:$hotel_id;
     $sql =  "SELECT id,name from ks_room_type where hotel_id =$hotel_id "  ;
     $arr = $objDbSelect->GetArray($sql);   
     return   $arr;
 }  
 
 function LoadRoom($id){    
     $objResponse = new xajaxResponse(); 
     $arrRoom =  GetRoomByHotel($id);
     $result = createCbbByArr($arrRoom);     
     return $objResponse->getXML();     
 }
 
 function createCbbByArr($arr,$nameOption='name',$valueOption='id',$cbbID='cbbTemp',$all=true){    
     
     $namedefault  =  $all?'--Tất cả--':'--Chọn--' ;
     $valuedefault =  $all?'':'-1' ;
     $result = "<select id='$cbbID' > <option value='$valuedefault'> $namedefault</option>";
     $count = count($arr);   
     for($i=0;$i<$count;$i++){
         $value = $arr[$i][$valueOption] ;
         $name = $arr[$i][$nameOption] ;
         $result .= "<option value='$value'> $name</option>";
     }
     $result.='</select>';
     return $result;     
 }
function ajx_viewchart($arrParams){

    global $objDbSelect;
    $objResponse = new xajaxResponse();
    include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
     
     $arrResult = getDataForChart($arrParams);
    $arrName = Array();        
    $arrLongdistance = Array();
    $max = 5;    
    $title = '1123';
    $format = 0;
    $sName='';
    $barArr = array();
    $barLab = array();
    $group =   $_SESSION['usergroup'];
    for($i=0;$i<count($arrResult);$i++){
        $sName = $arrResult[$i]['room_no'];
        $result = $arrResult[$i]['total'];
        $xx = new x_axis_label($sName, '#000000', 10, 315);            
        $barLab[] = $xx;
        $result = round($result,$format);
        array_push($arrLongdistance,$result); //mang quang duong
        if($max < $result ) $max = $result;
    }
    
   $barArr = $arrLongdistance;
   
    $bar = new bar_glass(1);            
    $chart = new open_flash_chart(); 
    $chart->set_title($title);             
    $bar->set_values($barArr ); 
    $range = getRangeMax($max);
    $y = new y_axis();
    $y->set_range(0,$max);
    $y->set_steps($range);
    
    
    $x_labels = new x_axis_labels();
    $x = new x_axis();
    $x_labels->set_labels($barLab );
    $x->set_labels($x_labels);


    $chart->add_element( $bar );
    $chart->set_x_axis($x);
    $chart->set_y_axis($y);
    
    
    $values = $chart->toPrettyString();
    
   $sScript = '_global.data=' . $values .';';                
    
   $objResponse->addScript($sScript);    
    $sScript1 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "divCharDev", "700", "360", "9.0.0", "expressInstall.swf", {"get-data":"_global.open_flash_chart_data"}, {"wmode":"opaque"});';
    $objResponse->addScript($sScript1);  
    $objResponse->addScript("$('#spnLoading').css('display', 'none');"); 
                       
    return $objResponse->getXML();    
}

function getDataForChart($arrParams){
    global $objDbSelect;
    
      $strroom = $arrParams['strroom'];
     $frmdate = $arrParams['frmdate'];
     $todate = $arrParams['todate'];
     $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
     //     $sql = "SELECT sum(if(hbr.rent_money is null,0,hbr.rent_money))+ sum( if(ads.money_sale is null , 0, ads.money_sale)) as total,r.room_no
// FROM ks_history_book_room as hbr join ks_room as r on r.id= hbr.room_id
// left join (select id_generate,sum(price_product_sale) as money_sale from ks_addservice where id_generate in
// (select id_generate from ks_history_book_room where room_id in ($strroom)  and end_time > '$frmdate' and end_time <'$todate')
// group by id_generate) as ads on hbr.id_generate = ads.id_generate where room_id in ($strroom)  and end_time > '$frmdate' and end_time <'$todate'";
    
    $sql = "SELECT sum(if(hbr.rent_money is null,0,hbr.rent_money))+ sum( if(ads.money_sale is null , 0, ads.money_sale)) as total,r.room_no

 FROM ks_history_book_room as hbr join ks_room as r on r.id= hbr.room_id

 left join (select id_generate,sum(price_product_sale) as money_sale from ks_addservice where id_generate in

 (select id_generate from ks_history_book_room where room_id = '1' and end_time > '2011-01-01' and end_time <'2011-12-12')

 group by id_generate) as ads on hbr.id_generate = ads.id_generate group by r.room_no"; 
 $result =  $objDbSelect->GetArray($sql); 
 return $result;
}
function getRangeMax(&$max){
    $lenth = strlen(round($max));
    $unit =  pow(10,$lenth-1);
    $numfirst = substr($max,0,1);
    $numfirst =  (int)$numfirst +1;
    $max =   $numfirst*$unit;
    $range = (int)($max/5);
    return $range;
}   
?>